package com.yty.demo._12_io;

import java.io.*;

/**
 * @author: yangtaoyao
 * @time: 2020/1/16 13:35
 * @description: 转换流
 */
public class StreamTransformTest {
    public static void main(String[] args) {
        String spath="D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\test1.txt";
        testInputStreamReader(spath);
        String tpath="D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\testOutputStreamWriter.txt";
        testOutputStreamWriter(tpath,"您好");
    }

    //字节输入流转化为字符输入流
    public static void testInputStreamReader(String path){
        try {
            FileInputStream fs=new FileInputStream(path);
            InputStreamReader isr=new InputStreamReader(fs);
            char[] c=new char[10];
            int len=0;
            while ((len=isr.read(c))!=-1){
                System.out.println(new String(c,0,len));
            }
            isr.close();
            fs.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //转换字节输出流为字符输出流
    public static void testOutputStreamWriter(String path,String txt){
        try {
            FileOutputStream out=new FileOutputStream(path);
            OutputStreamWriter osw=new OutputStreamWriter(out);
            String s=txt;
            osw.write(s);//void write(byte b[])
            osw.flush();
            osw.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
